package hk.polyu;

import java.io.*;
import java.util.*;

/**
 * CollectionBuilder class scan a directory and all files in it to build index.
 * 
 * @author hezongjian
 *
 */
public class CollectionBuilder {
	
	/**
	 * Path of the Collection
	 */
	String 			mPath;

	/**
	 * The information extracted from current collection.
	 */
	CollectionInfo	mCollection;
	
	/**
	 * The stop word list.
	 */
	Vector<String> 	mStopList = new Vector<String>();
	
	/**
	 * 
	 * @param mPath the path containing all the files to be indexed.
	 */
	public CollectionBuilder(String mPath) {
		super();
		this.mPath = mPath;
		
		this.mCollection = new CollectionInfo();
	}
	
	/**
	 * Read the stop word list from a file.
	 * @param file The file including the path.
	 */
	void readStopWord(String file) {
		String content = "";
		
		try {
			File f = new File(file);
			FileReader reader = new FileReader(f);
		    char[] chars = new char[(int) f.length()];
		    reader.read(chars);
		    content = new String(chars);
		    reader.close();
		} catch (IOException e) {
		    e.printStackTrace();
		    return;
		}
		
		// Tokenizer
		String [] stops = content.split("\\W");
		for (String s : stops) {
			Stemmer stm = new Stemmer();
			char [] arr = s.toCharArray();
			stm.add(arr, arr.length);
			stm.stem();
			this.mStopList.add(stm.toString().toLowerCase());
		}
		
	}
	

	/**
	 * This function create the index for all files
	 * @param bStemming Whether apply stemming to the terms.
	 * @param stopWords The file containing stop words, if you do not want stop words, just put it to null.
	 * @return CollectionInfo containing all information required to build index and queries.
	 */
	public CollectionInfo build(boolean bStemming, String stopWords) {
		
		File dir = new File(mPath);
		if (!dir.isDirectory())
			return null;
		
		// Firstly read the stop word list
		if (stopWords != null && !stopWords.isEmpty()) {
			this.readStopWord(stopWords);
		}
		
		File [] files = dir.listFiles();
		
		int docID = 0;
		
		for (File f : files) {
			if (! f.isFile() ||  f.isHidden())
				continue;
			System.out.println(f.getName());
			
			// begin parse
			DocumentInfo doc = new DocumentInfo(docID, f);
			if (doc.parse(bStemming, this.mStopList)) {
				this.mCollection.addDocument(docID, doc);
				docID++;
			}
		}
		return mCollection;
	}
	
	public CollectionInfo getCollectionInfo() {
		return this.mCollection;
	}



}
